<template>
  <el-form :rules="formRules" ref="myForm"  label-width="100px" class="login-box" :model="formData">
    <!-- 用户名 -->
    <el-form-item label="用户名" prop='username'>
      <el-input v-model="formData.username"> </el-input>
    </el-form-item>
    <!-- 密码 -->
    <el-form-item label="密码" prop="password">
      <el-input  v-model="formData.password"> </el-input>
    </el-form-item>
    <!-- 登陆按钮 -->
    <el-button type="primary" @click="submitForm('myForm')">登陆 </el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        username: "",
        password: "",
      },
      //校验规则
      formRules:{
        username:[
            { required: true, message: '用户名不能为空', trigger: 'blur' },
            { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
        ],
        password:[
            { required: true, message: '密码不能为空', trigger: 'blur' },
            { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  
  methods:{
    submitForm(myForm){
      this.$refs[myForm].validate((state)=>{
          if (state) {
        alert('登录成功')
        this.$router.push('./Login.vue')

      } else {
        this.$message({
          message:'账号密码错误',
          type:'error'
        })
      }
        }) 
    }
  }
};
</script>

<style scoped>
.login-box .el-input {
  border: 1px solid red;
}
</style>